package com.rizzo.android.content.processor;

import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import com.rizzo.android.content.RizzoContent;
import com.rizzo.android.content.http.StateHolder;


public abstract class RizzoContentProcessor {

    protected StateHolder stateHolder;
    protected String application;

    protected RizzoContentProcessor(StateHolder stateHolder, String application) {
        this.stateHolder = stateHolder;
        this.application = application;
    }

    protected ContentProviderOperation saveOrUpdate(Cursor cursor, ContentValues saveValues, ContentValues updateValues, String updateWhere,
                                                    String[] updateWhereArgs) {
        if (cursor.getCount() == 1) {
            // we have to update the contentProvider
            return ContentProviderOperation.newUpdate(RizzoContent.Quota.CONTENT_URI).withValues(updateValues).withSelection(updateWhere, updateWhereArgs)
                    .build();
        } else if (cursor.getCount() == 0) {
            // we have to save into the contentProvider
            Log.d("RizzoContentProcessor", "Inserting values: " + saveValues);
            return ContentProviderOperation.newInsert(RizzoContent.Quota.CONTENT_URI).withValues(saveValues).build();
        } else {
            throw new RuntimeException("Multiple entries found for a unique identifiable tupple! " +
                    new String[]{application, cursor.toString()}.toString());
        }
    }
}
